<template>
  <div class="createPost-container">
    <el-form ref="postForm" :model="postForm" class="form-container">

      <sticky :class-name="'sub-navbar published'" :v-index="1000">
        <el-button @click="resetChecked">清空</el-button>
        <el-button v-loading="loading" style="margin-left: 10px;" type="success" @click="submitForm">保存
        </el-button>
      </sticky>

      <div class="createPost-main-container">
        <el-form-item label-width="100px" label="">
          <el-tree
            ref="tree"
            :data="resourceTreeData"
            :default-checked-keys="checkedKeys"
            default-expand-all
            show-checkbox
            node-key="value"/>
        </el-form-item>

      </div>
    </el-form>

  </div>
</template>

<script>
import Sticky from '@/components/Sticky' // 粘性header组件
import { fetchGroupPermissions, createGroupPermissionRel } from '@/api/group'
import { fetchTreeData } from '@/api/permission'

export default {
  name: 'GroupDetail',
  components: { Sticky },
  props: {
    isEdit: {
      type: Boolean,
      default: false
    },
    currentId: {
      type: Number,
      default: undefined
    }
  },
  data() {
    return {
      postForm: {},
      loading: false,
      resourceTreeData: [],
      checkedKeys: []
    }
  },
  computed: {
    lang() {
      return this.$store.getters.language
    }
  },
  created() {
    this.fetchTreeData()
    if (this.isEdit) {
      const id = this.currentId
      this.fetchData(id)
    }
  },
  methods: {
    resetChecked() {
      this.$refs.tree.setCheckedKeys([])
    },
    fetchTreeData() {
      fetchTreeData().then(response => {
        this.resourceTreeData = response.data.data.tree_data
      }).catch(err => {
        console.log(err)
      })
    },
    fetchData(id) {
      fetchGroupPermissions({ id: id }).then(response => {
        this.$refs.tree.setCheckedKeys(response.data.data.permissions)
      }).catch(err => {
        console.log(err)
      })
    },
    submitForm() {
      const currentCheckedKeys = this.$refs.tree.getCheckedKeys()
      if (currentCheckedKeys.length <= 0) {
        this.$message({ message: '至少选择一个权限', type: 'error' })
        return
      }
      this.$refs.postForm.validate(valid => {
        if (valid) {
          this.loading = true
          createGroupPermissionRel({ 'groupId': this.currentId, 'permissionIds': currentCheckedKeys }).then(response => {
            const r = response.data
            if (r.code === 0) {
              this.$notify({
                title: '成功',
                message: '权限更新成功',
                type: 'success',
                duration: 2000
              })
              this.$emit('successGroupPermissions')
              this.setCheckedKeys = []
            } else {
              this.$message({ message: r.msg, type: 'error' })
            }
          })
          this.loading = false
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
@import "~@/styles/mixin.scss";
.createPost-container {
  position: relative;
  .createPost-main-container {
    padding: 40px 45px 20px 50px;
    .postInfo-container {
      position: relative;
      @include clearfix;
      margin-bottom: 10px;
      .postInfo-container-item {
        float: left;
      }
    }
  }
  .word-counter {
    width: 40px;
    position: absolute;
    right: -10px;
    top: 0px;
  }
}
</style>
